#include "BayesianHelper.h"
#include <fstream>

using namespace std;


#define	STOPWORDS	8
#define MAX_VALID_INT	10000

set<string> BayesianHelper::stopWords;

bool BayesianHelper::Serialize ( string fileName )
{
	ofstream f;
	f.open(fileName.c_str(),fstream::trunc);
	if ( !f.is_open() )
		return false;

	set<string>::iterator iter;
	for ( iter = stopWords.begin() ; iter != stopWords.end() ; iter++ )
	{
		int size = iter->size();
		f.write((char*)&size,sizeof(int));
		for ( int i = 0 ; i < size ; i++ )
			f << (*iter)[i];
	}

	f.flush();
	f.close();

	return true;
}


bool BayesianHelper::Deserialize ( string fileName )
{
	ifstream f;
	f.open(fileName.c_str());
	if ( !f.is_open() )
		return false;

	while ( !f.eof() )
	{
		int size = 0;		
		string tmp = "";

		f.read((char*)&size,sizeof(int));
		if ( size > 0 && !f.eof() )
		{
			char tmpChar;
			for ( int i = 0 ; i < size && !f.eof(); i++ )
			{
				f >> tmpChar;
				tmp += tmpChar;
			}

			stopWords.insert(tmp);
		}	
	}

	return true;
}


bool BayesianHelper::IsValid ( string word )
{
	int i = 0;
	for ( ; i < word.size() ; i++ )
		if ( !isdigit(word[i]) ) break;

	if ( i == word.size() ) 
	{
		int n = atoi(word.c_str());
		if ( n > MAX_VALID_INT )
			return false;
		else
			return true;
	}
			
	set<string>::iterator findWord = stopWords.find(word);
	if ( findWord != stopWords.end() )
		return false;

	return true;
}


unsigned char BayesianHelper::MakeReadable ( unsigned char c , unsigned char n )
{
	unsigned char retval = 0;
	
	if ( c == 195 )
	{
		if 	( (n >= 128 && n <= 133) || (n >= 161 && n <= 165) )
			retval = 'a';
		else if ( (n >= 136 && n <= 139) || (n >= 168 && n <= 171) )
			retval = 'e';
		else if ( (n >= 140 && n <= 143) || (n >= 172 && n <= 175) )
			retval = 'i';
		else if ( (n >= 146 && n <= 150) || (n >= 178 && n <= 181) )
			retval = 'o';
		else if ( (n >= 153 && n <= 156) || (n >= 185 && n <= 188 ) )
			retval = 'u';
		else if ( n == 135 || n == 167 )
			retval = 'c';
		else if ( n == 145 || n == 177 )
			retval = 'n';
	}

	return retval;
}


set<string> BayesianHelper::ParseWords ( string str )
{
	set<string> words;
	string word;

	for ( int i = 0 ; i < str.size() ; i++ )
		if ( isalnum(str[i]) )
			word += str[i];
		else if ( (i+1) < str.size() && MakeReadable(str[i],str[i+1]) ) 
		{
			word += MakeReadable(str[i],str[i+1]);
			i++;
		}
		else if ( word != "" )
		{
			for ( int j = 0 ; j < word.size() ; j++ )
				word[j] = tolower(word[j]);

			if ( IsValid(word) ) words.insert(word);
				word = "";
		}

	return words;
}
